﻿using MediatR;
using Medical.Api.Write.Application.Command.RecyclingCleaning;
using Medical.Api.Write.Application.Command.Wms.WmsOutCommand;
using Medical.Api.Write.Application.Command.Wms.WmsStorageCommand;
using Medical.Api.Write.Application.Command.Wms.WmsStoresCommand;
using Medical.ErrorCode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Medical.Api.Write.Dto.Wms;
using Medical.Api.Write.Application.Command.Wms.WmsLssueCommand;

namespace Medical.Api.Write.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WmsController : ControllerBase
    {

        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<WmsController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public WmsController(ILogger<WmsController> logger, IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }

        #region 入库

        /// <summary>
        /// 新增入库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddWmsStorage([FromBody] AddWmsStorageCommand command)
        {

            try
            {

                return await mediator.Send(command);

            }
            catch (Exception ex)
            {
                logger.LogError("添加回收登记异常处理" + ex.Message);
                throw;
            }

        }

        /// <summary>
        /// 审批入库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AuditStorageState([FromBody] AuditStorageStateCommand command)
        {

            try
            {
                command.AuditState = true;

                return await mediator.Send(command);

            }
            catch (Exception ex)
            {
                logger.LogError("审批入库异常" + ex.Message);
                throw;
            }

        }
        /// <summary>
        /// 撤销审批入库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UndoStorageState([FromBody] AuditStorageStateCommand command)
        {

            try
            {
                command.AuditState = false;

                return await mediator.Send(command);

            }
            catch (Exception ex)
            {
                logger.LogError("审批入库异常" + ex.Message);
                throw;
            }

        }

        /// <summary>
        ///逻辑删除入库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelBatchStorage([FromBody] DelBatchStorageCommand command)
        {

            try
            {

                return await mediator.Send(command);

            }
            catch (Exception ex)
            {
                logger.LogError("审批入库异常" + ex.Message);
                throw;
            }

        }

        /// <summary>
        ///修改入库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UptStorage([FromBody] UptStorageCommand command)
        {

            try
            {

                return await mediator.Send(command);

            }
            catch (Exception ex)
            {
                logger.LogError("修改入库异常" + ex.Message);
                throw;
            }

        }
        #endregion


        #region 存放
        /// <summary>
        /// 物品存放货架
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<bool>> MaterialStoreShelve([FromBody] StoreShelveCommand command)
        {

            try
            {

                return await mediator.Send(command);

            }
            catch (Exception ex)
            {
                logger.LogError("修改入库异常" + ex.Message);
                throw;
            }

        }

        /// <summary>
        /// 入库表Id获取详情后批量新增到待存放（存放表）
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<bool>> BatchAddStoresFromStorage([FromBody] WmsDto dto)
        {
            return await mediator.Send(new BatchAddStoresFromStorageCommand(dto.Id));
        }
        
        //发放，从已审批的转换

        #endregion


        #region 出库
        /// <summary>
        /// 出库审批（状态0改为5）
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<bool>> AuditOut([FromBody] AuditOutCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 出库撤销审批（状态5改为0）
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<bool>> UndoAuditOut([FromBody] UndoAuditOutCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 删除出库单，仅状态为0时允许删除
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<bool>> DeleteOut([FromBody] DeleteOutCommand command)
        {
            return await mediator.Send(command);
        }

        /// <summary>
        /// 修改出库主表，仅允许修改仓库和原因
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<bool>> UptOut([FromBody] UptOutDto dto)
        {
            return await mediator.Send(new UptOutCommand(dto));
        }


        /// <summary>
        /// 新增出库单（从存放表选择明细）
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<bool>> AddOutFromStores([FromBody] AddOutFromStoresCommand command)
        {
            return await mediator.Send(command);
        }

        #endregion


        #region 发放

        /// <summary>
        /// 新增发放主子表
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<bool>> AddLssue([FromBody] AddLssueDto dto)
        {
            dto.LssueState = 0;
            return await mediator.Send(new AddLssueCommand(dto));
        }




        /// <summary>
        /// 删除发放主子表，仅未发放状态可删除
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<bool>> DeleteLssue([FromBody] WmsDto dto)
        {

            return await mediator.Send(new DeleteLssueCommand(dto.Id));
        }

        /// <summary>
        /// 发放主表签收，仅未发放状态可签收
        /// </summary>
        /// <param name="dto">包含发放主表Id的DTO</param>
        /// <returns>签收结果</returns>
        [HttpPost]
        public async Task<ApiResult<bool>> SignLssue([FromBody] WmsDto dto)
        {
            // 只需传递Id即可，服务端会校验状态
            return await mediator.Send(new SignLssueCommand(dto.Id));
        }

        /// <summary>
        /// 根据出库单主键Id新增发放单
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<bool>> AddLssueFromOut([FromBody] AddLssueFromOutCommand command)
        {
            return await mediator.Send(command);
        }



        #endregion

    }
}

